<?php
class Controller extends ExtController {
	function index() {
		$this->page = new Page('catalogue', 'idn');
		$this->addChain('catalogue', 'Каталог');
	}
	function hardware() {
		$params = array('category'=>'string', 'manufacturer'=>'string', 'id'=>'int');
        extract($this->params($params));
		
		$this->addJS('catalog');
		
		$data = $this->catalog_data;
		
		//v_print($this->catalogue_tree);
		
		$chain = array('catalogue', 'hardware');
		if ($category) $chain[] = $category;
		if ($manufacturer) $chain[] = $manufacturer; 
		$this->addTreeChain($chain);
		
		if (!$category) {
			$this->_full_category();
			return;
		}
		
		if (!$manufacturer) {
			$this->_category($category);
			return;
		}

        if (!$id) {
            $data = $data['subitems'][$manufacturer];
            $this->_category($category, $manufacturer);
			return;
        }
		
		$item = new Hardware($id);
		$this->addChain($id, $item->getField('title'));
        $this->addViewData('item', $item->getFields());             
        $this->view_name = 'catalogue/item';
	}
	
	function _full_category() {
		$el = $this->getTreeElement('catalogue', 'hardware');
		$obj = new MyCatalogue($el['id']);
        $query = q::obj('MyCatalogue')->select()->where_in('id', $obj->getField('order'), FALSE)->end;
		$set = new MyCatalogueSet($query);
	    $fields = array(
            'MyCatalogue'=>array('title', 'idn', 'image')
        );
		
		$categories = $set->getArray($fields);
		//Расставляем url. Чёрт!
		foreach ($categories as $key=>$category) {
			$el = $this->catalogueIdTreeMap($category['id']);
			$categories[$key]['url'] = $el['url'];
		}
		
		
		$this->view_name = 'catalogue/full';
		$this->addViewData('categories', $categories);
		$this->addViewData('title', $obj->getField('title'));
	}
	
	function _category($idn, $manufacturer = FALSE) {
		$el = $this->getTreeElement('catalogue', 'hardware', $idn);
		$category = new MyCatalogue($el['id']);

		$this->view_name = 'catalogue/category';
		$this->addViewData('category', $category->getFields());
		$id = $category->getId();
		
		if (!$manufacturer) {		
			//Получение производителей
			$set = new MyCatalogueSet(q::obj('MyCatalogue')->select()->where_in('id', $category->getField('order'), FALSE)->end);
			$fields = array(
			'MyCatalogue'=>array('title', 'idn')
			);
			
			//Расстновка url
			$url_data = $this->catalog_data;
			$base_url = $url_data['idn'].'/';
			$url_data = $url_data['subitems']['hardware'];
			$base_url .= $url_data['idn'].'/';
			$url_data  = $url_data['subitems'][$category->getField('idn')];
			$base_url .= $url_data['idn'].'/';		
			
			$manufacturers = $set->getArray($fields);
			foreach ($manufacturers as $key=>$el) {
	            $manufacturers[$key]['url'] = $base_url.$el['idn'];			
			}
			$this->addViewData('manufacturers', $manufacturers);
		}
		
		/*
		 *  Получение списка товаров в данной категории
		 */
		if ($manufacturer) {
			$obj = new MyCatalogue($manufacturer, 'idn');
			$obj->getField('title');
			$id = $obj->getId();
		}
		
		$set = new HardwareSet(q::obj('Hardware')->select()->like('parents', '%#'.$id.'#%')->end);
		$set->joinByField('parent', 'MyCatalogue');
		
		$fields = array(
		          'Hardware'=>array('title', 'short_description', 'description', 'image', 'parents'),
				  'MyCatalogue as manufacturer'=>array('title')
		);
		$items = $set->getArray($fields);
		
		//Проставляем url
		foreach ($items as $key=>$item) {
			$url_data = $this->catalog_tree;
			$parent_ids = array_filter(explode('#', $item['parents']));
			$url = '';
			foreach ($parent_ids as $parent_id) {
				$url .= $url_data[$parent_id]['idn'].'/';
				$url_data = $url_data[$parent_id]['subitems'];
			}
			$items[$key]['url'] = $url.$item['id'];
		}
		
		/*
         *  Конец получения списка товаров в данной категории
         */
				
		$this->addViewData('items', $items);
	}
}
?>